# 本地完成
# 自动打包项目
# 一键上传
# 运行
import os
from utils.ssh import secure_crt
# 项目名称获取
from spring_boot.project_name import print_name
import logging


logging.basicConfig(level=logging.INFO)
# 输入项目目录  D:\CodePartice\demo
# springDirectory = input("请输入项目路径:")
springDirectory = r'D:\CodePartice\SpringProject\nubia'
os.chdir(springDirectory)  # 切换工作目录，相当于cd的命令
# 打包jar
os.system('mvn clean install -Dmaven.test.skip=true')
# ProjectName = input("请输入项目名字：")
# ProjectName = 'demo'
name_project = print_name(springDirectory)
springDirectoryJar = springDirectory + "\\" + "target" + "\\" + name_project + "-0.0.1-SNAPSHOT.jar"
name = name_project + "-0.0.1-SNAPSHOT.jar"
print("路径是：" + springDirectoryJar)
print("jarname：" + name)
crt = secure_crt('42.193.47.144', 'root', 'YuChen123456')
flag, res = crt.exec_cmd('netstat -lnp|grep 8080')
if not flag:
    crt.exec_cmd('systemctl stop firewalld')
    crt.exec_cmd('systemctl disable firewal\ld')
    crt.exec_cmd("mkdir -p /home/springboot")
    crt.copy_file(springDirectoryJar,
                  r'/home/springboot/' + name)
    crt.exec_cmd("cd /home/springboot")
    crt.yum('java -jar '+name+' &')
    # crt.yum('java -jar ' + name)
else:
    print("端口占用,请杀死进程")
